package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import java.util.stream.Collectors;


@RestController
@RequestMapping("dish")
public class DishController {
   @Autowired
   DishService dishService;
   @Autowired
   CategoryService categoryService;

   @Autowired
   DishFlavorService dishFlavorService;

   @PostMapping
   @CacheEvict(cacheNames = "dishCache",key = "#dishDto.categoryId")
   public R<String> save(@RequestBody DishDto dishDto){
      dishService.saveWithFlavor(dishDto);
      return R.success("添加成功");
   }

   @GetMapping("page")
   public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
      Page<Dish> pageInfo1=new Page<>(page, pageSize);
      LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class).like(StringUtils.isNotBlank(name), Dish::getName, name)
              .orderByDesc(Dish::getCreateTime)
              .orderByAsc(Dish::getSort);
      dishService.page(pageInfo1,queryWrapper);

      Page<DishDto> pageInfo2=new Page<>(page, pageSize);
      BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");

      List<Dish> dishList = pageInfo1.getRecords();
      List<DishDto> dishDtoList = dishList.stream().map(dish -> {
         DishDto dishDto = new DishDto();
         BeanUtils.copyProperties(dish, dishDto);

         Category category = categoryService.getById(dish.getCategoryId());
         dishDto.setCategoryName(category.getName());
         return dishDto;
      }).collect(Collectors.toList());
      pageInfo2.setRecords(dishDtoList);
      return R.success(pageInfo2);
   }

   @GetMapping("{id}")
   @Cacheable(cacheNames = "dishCache",key = "#id")
   public R<DishDto> getById(@PathVariable Long id){

      DishDto dishDto = dishService.getWithFlavor(id);
      return  R.success(dishDto);
   }

   @PutMapping
   @CacheEvict(cacheNames = "dishCache",allEntries = true)
   public R<String> update(@RequestBody DishDto dishDto) {
      dishService.updateWithFlavor(dishDto);
      return R.success("修改成功！");
   }

   @DeleteMapping
   @CacheEvict(cacheNames = "dishCache",allEntries = true)
   public R<String> delete(Long[] ids) {
      dishService.removeWithFlavor(ids);
      return R.success("删除成功");
   }

   @GetMapping("list")
   @Cacheable(cacheNames = "dishCache",key = "#dish.categoryId",unless = "#result.data.size()==0")
   public R<List<DishDto>> list(Dish dish) {

      LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class)
              .eq(Dish::getCategoryId, dish.getCategoryId());
      List<Dish> dishList = dishService.list(queryWrapper);

      List<DishDto> dishDtoList = new ArrayList<>();
      for (Dish dish1 : dishList) {
         DishDto dishDto=new DishDto();

         BeanUtils.copyProperties(dish1, dishDto);

         LambdaQueryWrapper<DishFlavor> wrapper = Wrappers.lambdaQuery(DishFlavor.class)
                 .eq(DishFlavor::getDishId, dishDto.getId());
         List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper);
         dishDto.setFlavors(dishFlavors);
         dishDtoList.add(dishDto);
      }
      return R.success(dishDtoList);
   }
}
